var myBehavior = require('../../behavior/my-behavior')

Page({
  behaviors: [myBehavior],
  data:  {
    title:[
      {itemTitle:'tab1',type:''},
      {itemTitle:'tab2',type: 0},
      {itemTitle:'tab3',type: 1},
      {itemTitle:'tab4',type: 2},
      {itemTitle:'tab5',type: 3},
      {itemTitle:'tab6',type: 6},
    ],
    currentIndex: 0,
    orderList: [],
    pageNum: 0,
    pageSize: 10,
    nomore: false,
    // scrollIntoView: ''
    scrollTop: 0
  },

  onLoad:  function  ()  {
  },
  
  onShow(){
    this._getOrderList(this.data.title[this.data.currentIndex].type);
    // 选择器数组  是否减去控制高
    this.getRect(['.top'])
  },

  onHide(){
    this.data.pageNum = 0
    this.data.pageSize = this.data.orderList.length
    this.data.orderList = []
    this.data.nomore = false
  },

  loadMore(){
    if (this.data.nomore)  return
    this._getOrderList(this.data.title[this.data.currentIndex].type)
  },

  
  viewDetail(e){
    wx.navigateTo({
      url: '/pages/bluetooth/index',
    })
  },

  jump(e){
    const { index } = e.currentTarget.dataset
    if (this.data.currentIndex === index) return
    this.setData({
      currentIndex: index,
      orderList: [],
      pageNum: 0,
      pageSize: 10,
      nomore: false,
    })
    this._getOrderList(this.data.title[this.data.currentIndex].type)
  },

  refreshList(e){
    const { id } = e.currentTarget.dataset
    this.setData({
      pageSize: this.data.orderList.length,
      orderList: [],
      pageNum: 0,
      nomore: false,
      scrollIntoView: 'item-' + id,
    })
    this._getOrderList(this.data.title[this.data.currentIndex].type)
  },


  // 获取订单列表
  _getOrderList(dataType){
    const token = 'eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjozLCJ1c2VyX2tleSI6ImQwZmMzYzFlLWJiMzQtNDU0Ni1hMzNiLWQ5Njc3Mzg4ZGFhMSIsInVzZXJuYW1lIjoiMTU3Nzk1MDcxMDEifQ.9cUBg4RpfwIA4lvsUninovlNVOCJfi4Y0Fib_6kyC66oKlDGDxbEeAWPRln1KBCTx27k_RbyOyZ7BZkX8PRUTQ'
    let data = {}
    const pageNum = this.data.pageNum + 1
    if (dataType === '') {
      data = {
        pageNum,
        pageSize: this.data.pageSize,
        paymentMethod: 0
      }
    }else{
      data = {
        pageNum,
        pageSize: this.data.pageSize,
        status: dataType,
        paymentMethod: 0
      }
    }
    wx.request({
      url: 'http://192.168.0.249:8080/mpms/v1/order/list',
      header: {
        Authorization: 'Bearer ' + token
      },
      data,
      success: res => {
        console.log(res);
        if (res.data.code === 200) {
          const total = res.data.total
          const pages = Math.ceil(total / this.data.pageSize)
          const oldOrderList = this.data.orderList
          if (pageNum >= pages) {
            this.setData({
              nomore: true,
            })
          }
          console.log(res.data.rows);
          const newRows = res.data.rows && res.data.rows.map(item => {
            const newGoodList = item.orderGoodsList.filter(goodsItem => {
              return goodsItem.requestId
            }).map(i => i.requestStatus)
            // 售后状态：0待商家处理  1待买家处理  2售后结束 3售后关闭 4退款中
            const uniqueGoodsListStatus = [...new Set(newGoodList)]
            // console.log(uniqueGoodsListStatus);
            return {
              ...item,
              orderAftersalesStatus: uniqueGoodsListStatus.length === 1 ? uniqueGoodsListStatus[0] : 0  // 多商品售后，售后状态的变化
            }
          })
          // console.log(newRows);
          res.data.total !== 0 && this.setData({
            // orderList: [...oldOrderList, ...res.data.rows],
            orderList: [...oldOrderList, ...newRows],
            pageNum
          })
        }else if (res.data.code === 500){
          wx.showToast({
            title: '获取订单列表失败',
            icon: 'none',
            mask: true,
          })
        }
      }
    })
  },

});